#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试CM3版本获取问题
"""

import requests
import json

BASE_URL = "http://127.0.0.1:8000"

def debug_cm3_version():
    """调试CM3版本获取问题"""
    print("=== 调试CM3版本获取问题 ===")
    
    # 测试1: 直接获取CM3最新版本
    print("\n1. 直接获取CM3最新版本...")
    try:
        response = requests.get(f"{BASE_URL}/ota/latest/cm3")
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print("✅ CM3最新版本获取成功")
            print(f"CM3版本: {data['data']['version']}")
        else:
            print(f"❌ 获取失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    # 测试2: 获取APP配置
    print("\n2. 获取APP配置...")
    try:
        response = requests.get(f"{BASE_URL}/v1/app-config")
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print("✅ APP配置获取成功")
            print(f"APP配置中的CM3版本: {data['data']['cm3_version']}")
        else:
            print(f"❌ 获取失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    # 测试3: 获取固件列表
    print("\n3. 获取固件列表...")
    try:
        response = requests.get(f"{BASE_URL}/ota/list")
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print("✅ 固件列表获取成功")
            cm3_firmwares = [item for item in data['items'] if item['cm3_version']]
            if cm3_firmwares:
                print("CM3固件列表:")
                for firmware in cm3_firmwares:
                    print(f"  ID: {firmware['id']}, 版本: {firmware['cm3_version']}, 创建时间: {firmware['created_at']}")
            else:
                print("  没有CM3固件")
        else:
            print(f"❌ 获取失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")

if __name__ == "__main__":
    debug_cm3_version() 